package org.nokatag.Connection;

import javax.servlet.http.HttpServletRequest;

import org.nokatag.system.Concest;
import org.nokatag.system.NLoadFun;

/**
 * 数据初始化连接
 * @author rebin
 *
 */
public class DBConnectionInit {
	/**
	 * 获取连接对象
	 * @param path
	 * @return
	 */
	public static DBConnectionInterface getDBConnectionInterface(String path){
		try{
			if(null==path || path.trim().length()<1)
				return null;
			NLoadFun nloadFun = new NLoadFun();
			return (DBConnectionInterface)nloadFun.getBean(path);
		}catch(Exception se){
		}
		return null;
	}
	/**
	 * 判断是否需要设置数据库连接对像
	 * @param request
	 * @return
	 */
	public static boolean isSetCon(HttpServletRequest request) {
		String resourcePath = getFileName(request);
		if (resourcePath == null) {
			return false;
		}
		//---------------例外-----------------------------
		for(String s:Concest.NO_CONNECTION_FILE_LW){
			String exp = resourcePath.toLowerCase();
			if(exp.equals(s))
				return true;
		}
		for (String s : Concest.NO_CONNECTION_FILE) {
			String exp = resourcePath.toLowerCase();
			if (exp.endsWith(s)) {// 静态文件不需要connection
				return false;
			}
		}
		return true;
	}
	private static String getFileName(HttpServletRequest request){
		String uri=request.getRequestURI();
		if(uri.indexOf(";")!=-1)
			uri=uri.substring(0,uri.indexOf(";")-1);
		if(uri!=null && uri.trim().length()>0){
			if(uri.lastIndexOf("/")<uri.length()-1)
				uri=uri.substring(uri.lastIndexOf("/")+1,uri.length());
			else
				return null;
		}
		return uri;
	}
}
